package com.urea.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.urea.common.core.domain.entity.SysUser;
import com.urea.common.core.domain.model.LoginUser;
import com.urea.common.utils.SecurityUtils;
import com.urea.common.utils.StringUtils;
import com.urea.system.constants.BusinessConstants;
import com.urea.system.domain.PayInfo;
import com.urea.system.domain.SysAccount;
import com.urea.system.domain.vo.CustomerVo;
import com.urea.system.domain.vo.HomeVo;
import com.urea.system.domain.vo.OrderVo;
import com.urea.system.domain.vo.TerminalVo;
import com.urea.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 类描述:
 *
 * @author fangXu
 * @version 1.0
 * @date 2023/8/25 14:29
 */
@Service
public class HomeServiceImpl implements IHomeService {

    @Autowired
    private ITerminalService terminalService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IBannerService bannerService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IPayInfoService payInfoService;
    @Autowired
    private IFleetService fleetService;

    @Override
    public HomeVo homeStatistics() {
        HomeVo vo = new HomeVo();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        Long userId = null;
        Long agentId = null;
        Long storeId = null;
        // 供应代理商
        if (BusinessConstants.AGENT_USER_TYPE.equals(user.getUserType())) {
            agentId = user.getUserId();
            // 车队
            vo.setFleetCount(terminalService.queryFleetCountByUidOrTid(userId, agentId,storeId));
            // 终端设备商
        } else if (BusinessConstants.TERMINAL_USER_TYPE.equals(user.getUserType())) {
            userId = user.getUserId();
            // 车队
            vo.setFleetCount(terminalService.queryFleetCountByUidOrTid(userId, agentId,storeId));
            // 管理人员
        } else if (BusinessConstants.ADMIN_TYPE.equals(user.getUserType())) {
            //　代理商
            vo.setAgentCount(userService.selectCountByUserType(BusinessConstants.AGENT_USER_TYPE));
            // 广告
            vo.setBannerCount(bannerService.count());
            // 车队
            vo.setFleetCount(fleetService.count());
        }else if (BusinessConstants.STORY_USER_TYPE.equals(user.getUserType())){
            storeId = user.getUserId();
            // 车队
            vo.setFleetCount(terminalService.queryFleetCountByUidOrTid(userId, agentId,storeId));
        }
        List<TerminalVo> countByCreateId = terminalService.getCountByCreateId(userId, agentId,storeId);

        // 订单统计
        OrderVo orderVo = orderService.queryStatisticsByDate(userId, agentId,storeId);
        // 门店数量
        vo.setStoreCount(terminalService.queryStoreCount(userId, agentId,storeId));
        // 消费情况
        List<PayInfo> payInfos = payInfoService.sevenPayInfoByDate(userId, agentId,storeId);
        // 代理商情况
        List<PayInfo> agentInfoList = payInfoService.sevenAgentInfoByDate(userId, agentId,storeId);
        vo.setOrderVo(orderVo);
        Long allCount = 0L;
        for (TerminalVo terminalVo : countByCreateId) {
            Integer online = terminalVo.getOnline();
            if (Objects.equals(online, 1)) {
                vo.setTerminalOnlineCount(terminalVo.getCountNo());
                allCount += terminalVo.getCountNo();
            } else {
                vo.setTerminalOfflineCount(terminalVo.getCountNo());
                allCount += terminalVo.getCountNo();

            }
        }
        vo.setTerminalCount(allCount);
        // 用户统计
        CustomerVo customerVo = customerService.queryStatisticsByDate(userId, agentId,storeId);
        // 用户信息
        vo.setUserCount(customerVo.getAllCount());
        vo.setCustomerVo(customerVo);
        vo.setPayInfoList(payInfos);
        vo.setAgentInfoList(agentInfoList);
        return vo;
    }
}
